//
//  UIImageView+WZB.m
//  LiveVideo
//
//  Created by WZB on 16/3/30.
//  Copyright © 2016年 jjjr. All rights reserved.
//

#import "UIImageView+WZB.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Extension.h"
#import <objc/runtime.h>
#import <Masonry.h>

@implementation UIImageView (WZB)

- (void)wzb_setImageCircle:(NSString *)url
{
    [self wzb_setImageCircle:url placeholdImage:[UIImage imageNamed:@"placeholder"]];
}

- (void)wzb_setImage:(NSString *)url {
    [self wzb_setImage:url placeholdImage:[UIImage imageNamed:@"placeholder"]];
}

// 根据url请求图片(带圆角)
- (void)wzb_setImageCircle:(NSString *)url placeholdImage:(UIImage *)placeholdImage {
    
    UIImage *placeholder = [placeholdImage circleImage];
    
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // 下载下来的image有值，就变圆，没有值就用占位图
        self.image = image ? [image circleImage]: placeholder;
    }];
}
// 根据url请求图片(不带圆角)
- (void)wzb_setImage:(NSString *)url placeholdImage:(UIImage *)placeholdImage {
    if (!url || url.length <= 0) {
        self.image = placeholdImage ? placeholdImage : [UIImage imageNamed:@"placeholder"];
        return;
    }
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholdImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (error) {
            self.image = placeholdImage ? placeholdImage : [UIImage imageNamed:@"placeholder"];
            return;
        }
        self.image = image;
    }];
}
@end
